package com.cyk.controller;

import com.cyk.entity.User;
import com.cyk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * restful 开发规范:
 * 1.原则: GET(查询)、POST(添加)、PUT(全字段更新)、PATCH(部分字段更新)、DELETE(删除)
 * 2.使用复数名词: user -> users、 car -> cars
 * 3.请求和响应指定: request: @RequestBody; response: @ResponseBody
 * 4.资源唯一标识需要通过路径传参，例如 id
 */

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2023-12-28 15:38:03
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;



    @PutMapping("id")
    public String updateUser(@PathVariable("id") Integer id, @RequestBody User user) {
        user.setId(id);
        userService.update(user);
        return "ok";
    }

    @GetMapping("/{id}")
    public User getUser(@PathVariable("id") Integer id) {
        return userService.queryById(id);
    }

    @PostMapping
    public String addUser(@RequestBody User user) {
        userService.insert(user);
        return "ok";
    }

    @DeleteMapping("/{id}")
    public String delUser(@PathVariable("id") Integer id) {
        userService.deleteById(id);
        return "ok";
    }

    @GetMapping
    public List<User> getUserList() {
        return userService.queryAll();
    }

}

